package com.lwp.test.myevent;

import com.lwp.test.myevent.event.MyEvent;
import com.lwp.test.myevent.listener.MyEventListener;
import org.apache.commons.collections4.MapUtils;

import java.util.Map;

/**
 * 事件上下文
 */
public class MyEventContext {

    /**
     * Java中可以通过synchronized、volatile、java concurrent类来实现共享变量的可见性
     */
    private static Map<String, MyEventListener> eventListenerMap = null;

    public static void setEventListenerMap(Map<String, MyEventListener> eventListenerMap) {
        MyEventContext.eventListenerMap = eventListenerMap;
    }


    /**
     * 发布事件
     */
    public static void publishEvent() {
        notifyTest();
    }

    /**
     * 通知
     */
    private static void notifyTest() {
        if (MapUtils.isEmpty(eventListenerMap)) {
            return ;
        }
        for(String beanId: eventListenerMap.keySet()){
            System.out.println("key:" + beanId);
            System.out.println("key:" + eventListenerMap.get(beanId));
            MyEventListener myEventListener = eventListenerMap.get(beanId);
//            myEventListener.notify("123");
            myEventListener.notify(new MyEvent("123"));
        }
    }
}
